package org.abc.fund.controller.factorController;

import lombok.RequiredArgsConstructor;
import org.abc.fund.entity.factorEntity.Factor;
import org.abc.fund.entity.factorEntity.NavData;
import org.abc.fund.service.factorService.FileParseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/data")
@RequiredArgsConstructor
public class DataSourceController {
    @Autowired
    private FileParseService fileParseService;

    @PostMapping("/file/upLoad")
    public ResponseEntity<Map<String, Object>> uploadFile(@RequestParam("file") MultipartFile file) {
        Map<String, Object> response = new HashMap<>();

        try {
            if (file.isEmpty()) {
                response.put("success", false);
                response.put("message", "文件为空");
                return ResponseEntity.badRequest().body(response);
            }

            String fileName = file.getOriginalFilename();
            if (fileName == null || (!fileName.endsWith(".csv") && !fileName.endsWith(".xlsx") && !fileName.endsWith(".xls"))) {
                response.put("success", false);
                response.put("message", "不支持的文件类型，请上传CSV或Excel文件");
                return ResponseEntity.badRequest().body(response);
            }

            // 解析文件并获取预览数据
            Map<String, Object> parseResult = fileParseService.parseFileForPreview(file);

            if (parseResult != null) {
                response.put("success", true);
                response.put("message", "文件上传成功");
                response.put("data", parseResult);
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "文件解析失败，请检查文件格式");
                return ResponseEntity.badRequest().body(response);
            }

        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "文件上传失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }

    @PostMapping("/file/save")
    public ResponseEntity<Map<String, Object>> saveFileData(@RequestParam("file") MultipartFile file) {
        Map<String, Object> response = new HashMap<>();

        try {
            List<Factor> result = fileParseService.parseAndSaveFile(file);
            if (result != null && !result.isEmpty()) {
                response.put("success", true);
                response.put("message", "数据保存成功，共处理 " + result.size() + " 条记录");
                response.put("data", result);
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "数据保存失败，请检查文件内容");
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "数据保存失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    @PostMapping("/Nav/save")
    public ResponseEntity<Map<String, Object>> saveNavData(@RequestParam("file") MultipartFile file) {
        Map<String, Object> response = new HashMap<>();

        try {
            List<NavData> result = fileParseService.saveNavDataFromFile(file);
            if (result != null && !result.isEmpty()) {
                response.put("success", true);
                response.put("message", "数据保存成功，共处理 " + result.size() + " 条记录");
                response.put("data", result);
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "数据保存失败，请检查文件内容");
                return ResponseEntity.badRequest().body(response);
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "数据保存失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
}
